<?php
namespace app\nadmin\controller;
use app\nadmin\model\Menu_M;
use think\Validate;
/**
 * @ explain 菜单相关类
 * @ time 2019/3/23
 * @ writer liuhao
 */

class Menu extends Base
{
    //模型实例
    protected $menuM;
    //每页记录数 默认为10
    protected $perpage = 10;

    //初始化模型
    protected function initialize()
    {
        $this->menuM = new Menu_M();
    }

    /*
     * @explain 系统列表页
     */
    public function index()
    {
       return $this->fetch();
    }

    //加载列表数据
    public function getMenuList()
    {
        //页码
        $page = $this->request->page?$this->request->page:1; //页码
        $perpage = $this->request->limit ? $this->request->limit :$this->perpage; //每页记录数
        //数据验证
        $rule = [
            'page' => 'number|gt:0',
            'perpage' => 'number|gt:0',
        ];
        $msg = [
            'page.integer' => '页码参数必须为整数',
            'page.gt' => '页码参数必须大于0',
            'perpage.integer' => '每页记录数参数必须为整数',
            'perpage.gt' => '每页记录数参数必须大于0',
        ];
        $data = ['page'=>$page,'perpage'=>$perpage];
        $validate = Validate::make($rule,$msg);
        $re = $validate->check($data);
        if(!$re)
        {
            return echojson($validate->getError(),0);
        }

        $start = ($page-1) * $perpage;
        $limit = $start.','.$perpage;


        $field = "id,title,module,controller,action,pid,lev,d_type,status,sort,ctime";
        $list = $this->getMore($field,$where = [],$order = [],$limit);
        //获取个数
        $count = $this->menuM->getCount();
        return laytable($count,$list);
    }

    //查找多个数据
    public function getMore($field='',$where = [],$order = [],$limit='')
    {
        $list = $this->menuM->getMore($field,$where,$order,$limit);
        return $list;
    }
    //编辑操作获取单个数据
    public function menuEdit()
    {
        $id = $this->request->id;
        $validate = Validate::make(['id'=>'integer|gt:0'],['id.integer'=>'参数异常 E001','id.gt'=>'参数异常 E002']);
        $re = $validate->check(['id'=>$id]);
        if(!$re)
        {
            return echojson($validate->getError(),0);
        };

        $where = ['id'=>$id];
        $field = "id,title,module,controller,action,pid,lev,d_type,status,sort,ctime";
        $list = $this->menuM->getOne($field,$where);
        return echojson('success',1,$list);
    }

    //编辑操作
    public function doEdit()
    {
        //参数
        $id = $this->request->id;
        $status = $this->request->status;
        $title = $this->request->title;
        $module = $this->request->module;
        $controller = $this->request->controller;
        $action = $this->request->action;
        $d_type = $this->request->d_type;
        $pid = $this->request->pid;
        $lev = $this->request->lev + 1;

        //验证
        $rule = [
            'id' => 'require|gt:0|number',
            'status' => 'in:0,1',
            'title'=> 'require|chsDash|min:1|max:16', //汉字字母数字下划线
            'module' =>'require|alphaDash|min:2|max:50', //字母数字下划线
            'controller' =>'require|alphaDash|min:2|max:50', //字母数字下划线
            'action' =>'require|alphaDash|min:1|max:50', //字母数字下划线
            'd_type' => 'in:0,1,2',
            'pid' => 'require|number',
            'lev' => 'require|in:1,2,3',
        ];
        $msg = [];
        $data = [
            'id'=>$id,
            'status'=>$status,
            'title'=>$title,
            'module'=>$module,
            'controller'=>$controller,
            'action'=>$action,
            'd_type'=>$d_type,
            'pid'=>$pid,
            'lev'=>$lev
        ];
        $validate = Validate::make($rule,$msg);
        $re = $validate->check($data);
        if(!$re)
        {
            return echojson($validate->getError(),0);
        }

        $where = ['id'=>$id];
        $re = $this->menuM->editOne($data,$where);
        if($re){
            return echojson("修改成功",1);
        }else{
            return echojson("修改失败",0);
        }
    }

    //添加单条操作
    public function doAdd()
    {
        //参数
        $status = $this->request->status;
        $title = $this->request->title;
        $module = $this->request->module;
        $controller = $this->request->controller;
        $action = $this->request->action;
        $d_type = $this->request->d_type;
        $pid = $this->request->pid;
        $now = date('Y-m-d H:i:s',time());
        $lev = $this->request->lev + 1;


        //验证
        $rule = [
            'status' => 'in:0,1',
            'title'=> 'require|chsDash|min:1|max:16', //汉字字母数字下划线
            'controller' =>'require|alphaDash|min:2|max:50', //字母数字下划线
            'action' =>'require|alphaDash|min:1|max:50', //字母数字下划线
            'd_type' => 'in:0,1,2',
            'pid' => 'require|number',
            'lev' => 'require|in:1,2,3|number',
        ];
        $msg = [];
        $data = compact('status','title','module','controller','action','d_type','pid','lev');
        $validate = Validate::make($rule,$msg);
        $re = $validate->check($data);
        if(!$re)
        {
            return echojson($validate->getError(),0);
        }
        //操作
        $data['ctime'] = $now;
        $re = $this->menuM->insertOne($data);
        if($re){
            return echojson("添加成功",1);
        }else{
            return echojson("添加失败",0);
        }
    }
    //编辑 状态操作
    public function changeStatus()
    {
        //参数
        $id = $this->request->id;
        $status = $this->request->status;
        //验证参数
        $rule = [
            'id' => 'require|gt:0|number',
            'status' => 'in:0,1',
        ];
        $msg = [
            'id.integer' => '参数ID必须为整数',
            'id.gt '=> '参数ID必须大于0',
            'status.in' => '参数status只能为0,1之间',
        ];
        $data = [
            'id' => $id,
            'status' => $status,
        ];
        $validate = Validate::make($rule,$msg);
        $re = $validate->check($data);
        if(!$re)
        {
            return echojson($validate->getError(),0);
        }

        //编辑
        $where = ['id'=>$id];
        $data = ['status'=>$status];
        $result = $this->menuM->insertOne($data,$where);
        if($result)
        {
            return echojson("修改成功",1);
        }else{
            return echojson("修改失败",0);
        }
    }

    //删除单条记录
    public function delOne()
    {
        $id = $this->request->id;
        //验证数据
        $validate = Validate::make(['id' => 'integer|gt:0'],[]);
        $re = $validate->check(['id'=>$id]);
        if(!$re)
        {
            return echojson($validate->getError(),0);
        }

        $where = ['id'=>$id];
        $result = $this->menuM->delOne($where);
        if($result)
        {
            return echojson('删除成功',1);
        }else{
            return echojson('删除失败',0);
        }
    }
    //删除多条记录
    public function delMore()
    {
        $idStr = $this->request->idStr;
        //验证规则
        $rule = [
            'idStr' => ['regex' => '/^(([1-9]\d*)|(([1-9]\d*,)+[1-9]\d*))$/','require'],
        ];
        $validate = Validate::make($rule,[]);
        $re = $validate->check(['idStr'=>$idStr]);
        if(!$re)
        {
            return echojson($validate->getError(),0);
        }

        $where = "id in ($idStr)";
        $result = $this->menuM->delMore($where);
        if($result)
        {
            return echojson('删除成功',1);
        }else{
            return echojson('删除失败',0);
        }
    }

    //修改排序
    public function editSort()
    {
        $id = $this->request->id;
        $sort = $this->request->sort; //排序字段数据

        $rule = [
            'id'=>'require|gt:0|number',
            'sort' => 'between:0,999',
        ];
        $data = [
            'id'=>$id,
            'sort'=>$sort,
        ];

        $validate = Validate::make($rule,[]);
        $re = $validate->check($data);
        if(!$re)
        {
            return echojson($validate->getError(),0);
        }

        $result = $this->menuM->editOne(['sort'=>$sort],['id'=>$id]);
        if($result)
        {
            return echojson('修改成功',1);
        }else{
            return echojson('修改失败',0);
        }
    }

    /*
     * @explain 查询菜单所有数据并完成无限极分类排序
     */
    public function getMenuCate()
    {
        $field = 'id,pid,lev,title';
        $list = $this->getMore($field,$where = [],$order = [],$limit='');

        $list = getTree($list,$pid=0); //无限极分类

        return echojson('OK',1,$list);
    }
}